<?php

class CategoryController extends Controller
{
   /**
    * Set layout
   */
   public $layout = '//layouts/cms/main';
	public function actionIndex()
	{
	   $page = Yii::app()->request->getParam('page',1); // Lấy số trang
      $limit = Yii::app()->request->getParam('limit',10); // Lấy số dòng hiển thị trên grid
      $start = $limit*$page - $limit; // Thứ tự bản ghi bắt đầu lấy  
	   $model = new Category();  
      $object = $model->findAll(array('offset' => $start,
                                       'limit' => $limit));
      $list = array();
      $total_record = $model->count();
      if(!is_numeric($page)) $page = 1;
      if(!is_numeric($limit)) $limit = 10;
      if( $total_record >0 ) {
      	$total_pages = ceil($total_record/$limit);
      } else {
      	$total_pages = 0;
      }
      if(count($object) > 0){
         foreach($object as $item){
            $list[$item['cat_id']] = $item->attributes;
         }
      }
      
		$this->render('index',array('list' => $list,
                                  'limit' => $limit,
                                  'start' => $start,
                                  'page' => $page,
                                  'total_pages' => $total_pages,
                                  'total_record' => $total_record));
	}
   
   /**
    * Display pages create
   */
   public function actionCreate(){
      $model = new Category();
      $message = '';
      $action_user = Yii::app()->request->getParam('action_user',''); // Biến xác định user ấn apply hay save
      $all_cate = $model->getAll();
      $tree_cate = $model->getTree($all_cate);
      if(isset($_POST['Category'])){
         $model->attributes = $_POST['Category'];
         if($model->validate()){
            $model->save();
            $message = 'Thêm mới thành công';
            $tree_cate = $model->getTree($all_cate);
            if($action_user == 'save'){
               $this->redirect('/admin/'.$this->id);
            } 
         }
         //CVarDumper::dump($model->attributes,10,true);die();
      }
      $this->layout = '//layouts/cms/create';
      $this->render('create',array('model' => $model,
                                    'tree_cate' => $tree_cate,
                                    'message' => $message));
   }
   
   /**
    * Action edit member
   */
   public function actionEdit(){
      $session_user = Yii::app()->session['session_user'];
      $action_user = Yii::app()->request->getParam('action_user',''); // Biến xác định user ấn apply hay save
      $message = '';
      $id = Yii::app()->request->getParam('id',0);
      $model = Category::model()->findByPk($id);
      if(isset($_POST['Category'])){
         $model->attributes = $_POST['Category'];
            if($model->validate()){
               $message = "Cập nhật thành công";
               $model->save(false);
               if($action_user == 'save'){
                  $this->redirect('/index.php/admin/'.$this->id);
               }   
            }
            
      }
      $all_cate = $model->getAll();
      $tree_cate = $model->getTree($all_cate);
      $this->layout = '//layouts/cms/create';
      $this->render('edit',array('model' => $model,
                                 'session_user' => $session_user,
                                 'tree_cate' => $tree_cate,
                                 'message' => $message
                                 ));
   }
   
   
    /**
    * Delete record
   */
	public function actionDelete(){
      $error = 1;
      $record_affected = 0;
      $list_id = Yii::app()->request->getParam('list_id',array());      
      if(count($list_id) > 0){
         $sql = 'DELETE FROM category WHERE cat_id IN(-1';
         foreach ($list_id as $item){
            if(is_numeric($item))
               $sql .= ',' . $item;
         }
         $sql .= ')';
         $db = Yii::app()->db;
         $cmd = $db->createCommand($sql);
         $record_affected = $cmd->execute();
         if($record_affected >= 0){
            $error = 0;
         }else{
            $error = 1;
         }
      }
      
      echo json_encode(array('error' => $error,
                             'record_affected' => $record_affected)); 
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}